const Router = require('@koa/router')//导入路由包
const { UserModel } = require('../database/index')
const { Op } = require("sequelize")
const router = new Router();//实例化路由

router.get('/getAllUserByPage', async (ctx, next) => {
    const { type, username, currentPage, pageSize } = ctx.query;
    const condition1 = username ? { username: { [Op.like]: `%${username}%` } } : null;
    const condition2 = type ? { type: { [Op.eq]: type } } : null;

    const { count, rows } = await UserModel.findAndCountAll({
        where: Object.assign({}, condition1, condition2),

        offset: (currentPage - 1) * pageSize,
        limit: parseInt(pageSize)
    },)
    ctx.body = {
        code: 200,
        msg: "获取数据成功!!!",
        data: {
            rows: rows,
            count: count
        }
    };

});

//删除
router.post('/handleDelete', async (ctx, next) => {
    const { userId } = ctx.request.body;

    try {
        const result = await UserModel.destroy({
            where: {
                userId: userId
            }
        });

        if (result) {
            ctx.body = {
                code: 200,
                msg: "删除成功"
            };
        } else {
            ctx.body = {
                code: 201,
                msg: "删除失败"
            };
        }
    } catch (error) {
        ctx.body = {
            code: 500,
            msg: "删除失败"
        };
    }
});

// 添加用户
router.post("/adduser", async (ctx, next) => {
    const { username, password, phone, type, sex, status } = ctx.request.body;
    console.log("测试", username, password, phone, type, sex, status);

    const avator = ctx.files[0].filename;//获取上传的文件名
    const result = await UserModel.create({
        username,
        password,
        phone,
        type,
        avator,
        sex,
        status,
    });
    console.log(result.toJSON());
    if (result) {
        ctx.body = {
            code: 200,
            msg: "添加成功",
        };
    } else {
        ctx.body = {
            code: 500,
            msg: "添加失败",
        };
    }


});
// 更新用户信息
router.post("/updateUser", async (ctx, next) => {
    const { userId, username, phone, type, sex, status } = ctx.request.body;
    // console.log("测试", username, phone, type, sex, status);
    const avator = ctx.files[0].filename;//获取上传的文件名

    try {
        const result = await UserModel.update(
            { username, phone, type, sex, status, avator },
            { where: { userId: userId } }
        );
        console.log(result);
        if (result) {
            ctx.body = {
                code: 200,
                msg: "更新成功",
            };
        } else {
            ctx.body = {
                code: 500,
                msg: "更新失败",
            };
        }

    } catch (error) {
        console.log(error);
        ctx.body = {
            code: 501,
            msg: "更新失败",
        };
    }
});
//登录
router.post("/login", async (ctx, next) => {
    const { username, password } = ctx.request.body;
    console.log(111, username, password);
    try {
      const user = await UserModel.findOne({ where: { username, password } });
      // console.log(user);
      if (user) {
        console.log(user.toJSON());
        ctx.body = {
          code: 200,
          msg: "success",
          data: user.toJSON(),
        };
      } else {
        ctx.body = {
          code: 201,
          msg: "用户名或密码错误",
        };
      }
    } catch (error) {
      ctx.body = {
        code: 500,
        msg: "登录失败",
      };
    }
  });
//注册
router.post('/register', async (ctx, next) => {
    const { username, password } = ctx.request.body
    console.log(111, username, password)
    try {
        const result = await UserModel.create({ username, password })
        ctx.body = {
            code: 200,
            msg: '注册成功'
        }
    } catch (error) {
        ctx.body = {
            code: 500,
            msg: '注册失败'
        }
    }

})


module.exports = router